package com.stylefeng.guns.rest.modular.App.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.rest.Tools.Result;
import com.stylefeng.guns.rest.common.annotion.SSO;
import com.stylefeng.guns.rest.modular.App.model.TaskInf;
import com.stylefeng.guns.rest.modular.App.service.ITaskInfService;
import com.stylefeng.guns.rest.validater.Search;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 任务信息表
 *
 * @author 张亚林 @Date 2018-11-12 17:05
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/taskInf"})
@Api(value = "任务信息表操作相关api",tags = {"任务信息表操作接口"})
public class TaskInfController extends BaseController {
  @Autowired
  private ITaskInfService taskInfService;

  /**
   * 删除任务信息表数据
   * @param tiId
   * @param trrId
   * @return
   */
  @ApiOperation(value = "拒绝任务接口", notes = "通过拒绝原因和任务信息拒绝接受任务", httpMethod = "POST")
  @PostMapping(value = "/refuseTask")
  @ResponseBody
  @SSO
  public Result refuseTask( @ApiParam(name = "tiId", value = "任务ID", required = true) @RequestParam(value = "tiId") String tiId,
      @ApiParam(name = "trrId", value = "拒绝原因ID", required = true) @RequestParam(value = "trrId") String trrId) {
      try {
          taskInfService.refuseTask(tiId,trrId,getToken());
          return Result.success( );
      }catch(Exception e){
          return Result.error(e.getMessage(),500100);
      }
  }
    /**
     * 分页获取我的任务
     * @param taskInf
     * @return
     */
    @ApiOperation(value = "分页获取我的任务", notes = "分页获取我的任务", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result pageList(@RequestBody @ApiParam(name = "taskInf", value = "线路城市信息对象")@Validated(value= Search.class) TaskInf taskInf, BindingResult result) {
        if(result.hasErrors()){
            return Result.error("必要参数不全！",200);
        }
        try {
            Pager pager=new Pager();
            pager.setPageNumber(taskInf.getPageNum());//页码从1 开始
            pager.setPageSize(taskInf.getPageSize());
            pager.setObj(taskInf);
            return Result.success(taskInfService.pageListTaskInf(pager,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }

    /**
     * 查询我的当前任务 执行中是第一个 未执行按预约时间最近排序
     * @param taskInf
     * @return
     */
    @ApiOperation(value = "首页显示我的当前任务", notes = "首页显示我的当前任务", httpMethod = "POST")
    @PostMapping(value = "/firstList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result firstList( @RequestBody @ApiParam(name = "taskInf", value = "线路城市信息对象")TaskInf taskInf) {
        try {

            return Result.success(taskInfService.selectFirstListTaskInf(taskInf,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }

    /**
     * 开始任务
     * @param tiId  任务单ID
     * @param addr  上车地址
     * @param pos   上车经纬度get
     * @return
     */
    @ApiOperation(value = "开始任务", notes = "开始任务", httpMethod = "POST")
    @PostMapping(value = "/startTask", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result startTask(
            @ApiParam(name = "tiId", value = "任务Id", required = true) @RequestParam(value = "tiId") String tiId,
            @ApiParam(name = "addr", value = "地址", required = true) @RequestParam(value = "addr") String addr,
            @ApiParam(name = "pos", value = "经纬度", required = true) @RequestParam(value = "pos") String pos) {
        try {
            return Result.success(taskInfService.updateStartTaskInf(tiId,addr,pos,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }


    /**
     * 结束任务
     * @param tiId  任务单ID
     * @param addr  下车地址
     * @param pos   下车经纬度
     * @return
     */
    @ApiOperation(value = "结束任务", notes = "结束任务", httpMethod = "POST")
    @PostMapping(value = "/endTask", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result endTask(
            @ApiParam(name = "tiId", value = "任务Id", required = true) @RequestParam(value = "tiId") String tiId,
            @ApiParam(name = "addr", value = "地址", required = true) @RequestParam(value = "addr") String addr,
            @ApiParam(name = "pos", value = "经纬度", required = true) @RequestParam(value = "pos") String pos) {
        try {
            return Result.success(taskInfService.updateEndTaskInf(tiId,addr,pos,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }

    private String getToken(){
        return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
